const router = require("koa-router")();
const { query } = require("./../config/dbPool");

const {
  COURSELIST_SQL,
  ELECTIVEUSERID_SQL,
  ELECTIVEUSERIDINFO_SQL,
  COURSEINFO_SQL,
  COURSEADD_SQL,
  COURSEUPDATE_SQL,
  COURSEDELETE_SQL,
} = require("./../config/dbSQL");

router.prefix("/course");

/**
 * 获取全部课程信息
 * @url /course/info
 */
router.post("/list", async (ctx) => {
  const request = ctx.request.body;
  const { course_id } = request;
  let res = {};
  if (course_id) {
    res = JSON.parse(JSON.stringify(await query(COURSEINFO_SQL, [course_id])));
  } else {
    res = JSON.parse(JSON.stringify(await query(COURSELIST_SQL)));
  }
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      data: res.data,
      message: "获取成功",
    };
  }
});

/**
 * 获取带用户是否选课标识的全部课程信息
 * @url /course/user/list
 */
router.post("/user/list", async (ctx) => {
  const request = ctx.request.body;
  const token = ctx.request.header.token;
  const { course_id } = request;
  let res = {};
  if (course_id) {
    res = JSON.parse(
      JSON.stringify(await query(ELECTIVEUSERIDINFO_SQL, [token,course_id, token]))
    );
  } else {
    res = JSON.parse(JSON.stringify(await query(ELECTIVEUSERID_SQL, [token,token])));
  }
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      data: res.data,
      message: "获取成功",
    };
  }
});

/**
 * 根据course_id获取课程信息
 * @url /course/:course_id/info
 */
router.get("/:course_id/info", async (ctx) => {
  const course_id = ctx.params.course_id;
  const res = JSON.parse(
    JSON.stringify(await query(COURSEINFO_SQL, [course_id]))
  );
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      data: {
        ...res.data[0],
      },
      message: "获取成功",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "获取失败",
    };
  }
});

/**
 * 添加课程
 * @url /course/add
 */
router.post("/add", async (ctx) => {
  const request = ctx.request.body;
  const { course_id, course_name, course_credit, spare_time } = request;
  const queryData = {
    course_id,
    course_name,
    course_credit,
    spare_time,
  };
  if (course_id) {
    const data = JSON.parse(
      JSON.stringify(await query(COURSEINFO_SQL, [course_id]))
    );
    if (data.result && data.data.length !== 0) {
      ctx.body = {
        status: 200,
        result: false,
        message: "课程已存在",
      };
      return;
    }
    const res = await query(COURSEADD_SQL, queryData);
    if (res.result) {
      ctx.body = {
        status: 200,
        result: true,
        message: "添加成功",
      };
    } else {
      ctx.body = {
        status: 200,
        result: false,
        message: "添加失败",
      };
    }
  }
});

/**
 * 修改课程
 * @url /course/:course_id/info
 */
router.put(`/:course_id/info`, async (ctx) => {
  const request = ctx.request.body;
  const course_id = ctx.params.course_id;
  const { course_name, course_credit } = request;
  const queryData = [course_name, course_credit, course_id];
  const res = await query(COURSEUPDATE_SQL, queryData);
  if (res.result) {
    ctx.body = {
      status: 200,
      result: true,
      message: "修改成功",
    };
  } else {
    ctx.body = {
      status: 200,
      result: false,
      message: "修改失败",
    };
  }
});

/**
 * 删除课程
 * @url /course/:course_id/info
 */
router.delete(`/:course_id/info`, async (ctx) => {
  const course_id = ctx.params.course_id;
  if (course_id) {
    const res = await query(COURSEDELETE_SQL, [course_id]);
    if (res.result) {
      ctx.body = {
        status: 200,
        result: true,
        message: "删除成功",
      };
    } else {
      ctx.body = {
        status: 200,
        result: false,
        message: "删除失败",
      };
    }
  }
});

module.exports = router;
